package com.soft.collateral.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.Roles;
import com.soft.collateral.entity.User;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.RolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户职能表 前端控制器
 * </p>
 *
 * @author liubin
 * @since 2025-07-25
 */
@RestController
@RequestMapping("/admin/role")
@Slf4j
@Api(tags = "后台系统-角色管理接口")
@RequiredArgsConstructor
public class RolesController {

    private final RolesService roleService;

    @ApiOperation("获取角色列表")
    @GetMapping("list")
    public Result list(){
        List<Roles> list = roleService.list();
        if(!CollectionUtils.isEmpty(list)){
            return new Result(true , MessageConstant.GET_ROLE_SUCCESS , list);
        }
        return new Result(false , MessageConstant.GET_ROLE_FAIL);
    }

    @ApiOperation("根据id查询用户的角色")
    @GetMapping("getRolesByUserId/{id}")
    public Result getRolesByUserId(@PathVariable("id") int userId){
        List<Integer> roleIds = roleService.getRolesByUserId(userId);
        if(!CollectionUtils.isEmpty(roleIds)){
            return new Result(true , MessageConstant.GET_ROLE_SUCCESS , roleIds);
        }
        return new Result(false , MessageConstant.GET_ROLE_FAIL);
    }

    @ApiOperation("新增角色种类")
    @PostMapping("create")
    public Result create(@RequestBody Roles role){
        boolean save = roleService.save(role);
        if(save){
            return new Result(true ,MessageConstant.SAVE_ROLE_SUCCESS);
        }
        return new Result(false ,MessageConstant.SAVE_ROLE_FAIL);
    }

    @ApiOperation("更新角色信息")
    @PutMapping("update")
    public Result update(@RequestBody Roles role) {
        boolean flag = roleService.updateById(role);
        if(flag){
            return new Result(true ,MessageConstant.UPDATE_ROLE_SUCCESS);
        }
        return new Result(false ,MessageConstant.UPDATE_ROLE_FAIL);
    }

    @ApiOperation("根据id删除角色")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = roleService.deleteById(id);
        if(flag){
            return new Result(true ,MessageConstant.DELETE_ROLE_SUCCESS);
        }
        return new Result(false ,MessageConstant.DELETE_ROLE_FAIL);
    }

}
